package cn.msi.customer.service;

import java.util.Date;

import cn.common.manager.SessionManager;
import cn.common.util.GeneralUtils;
import cn.msi.customer.domain.Customer;
import cn.msi.customer.domain.User;
import cn.msi.customer.mapper.CustomerMapper;
import cn.msi.customer.mapper.UserMapper;

/**
 * 客户资源业务处理类
 * 
 * @author 孙树林
 * 
 */
public class CustomerService {

	private CustomerMapper customerMapper;

	private UserMapper userMapper;

	/**
	 * 客户注册业务
	 * 
	 * @throws Exception
	 */
	public void register(Customer customer) throws Exception {
		Date createTime = new Date();
		// 用户注册为网站注册用户
		customer.setCustType("R");
		// 生成用户编码
		// customer.setCustCode(custCode);
		// 用户注册时间
		customer.setCreateTime(createTime);
		// 用户为可用状态
		customer.setCustState("S");
		// 保存客户资料
		customerMapper.insert(customer);

		// 生成客户的管理用户账号及密码同时将信息发送到客户邮箱
		User user = new User();
		user.setCustId(customer.getCustId());
		user.setUserType("M");
		// 用户编码
		// user.setUserCode("");
		user.setUserName("管理员");
		user.setLoginName("sysadmin");
		// @TODO 根据规则生成用户密码并加密
		user.setPassword("123");
		user.setCreateTime(createTime);
		user.setUserState("S");

		userMapper.insert(user);
	}

	/**
	 * 调研客户查找自己的客户资料
	 * 
	 * @param customer
	 * @throws Exception
	 */
	public Customer findCustResource(Customer customer) throws Exception {
		Integer id = customer.getCustId();
		if (id != null) {
			customer = customerMapper.selectById(id);
		}
		return customer;
	}

	/**
	 * 调研客户编辑自己的客户资料业务处理方法
	 * 
	 * @param customer
	 * @throws Exception
	 */
	public void redactCustResource(Customer customer) throws Exception {
		Integer id = customer.getCustId();
		if (id != null) {
			customerMapper.update(customer);
		} else {
			User user = (User) SessionManager.getAttr("user");
			customer.setCustLinkId(user.getCustId());
			customer.setUserId(user.getUserId());
			// 被调研客户
			customer.setCustType("R");
			// 生成用户编码
			// customer.setCustCode(custCode);
			// 用户注册时间
			customer.setCreateTime(new Date());
			// 用户为可用状态
			customer.setCustState("S");
			customerMapper.insert(customer);
		}
	}

	/**
	 * 调研客户删除自己的客户资料业务处理方法
	 * 
	 * @param customer
	 * @throws Exception
	 */
	public void deleteCustResource(Customer customer) throws Exception {
		customer.setCustState("D");
		customerMapper.update(customer);
	}

	/**
	 * 调研客户查询客户资源列表
	 * 
	 * @param customer
	 * @return
	 * @throws Exception
	 */
	public String searchCustResource(Customer customer) throws Exception {
		User user = (User) SessionManager.getAttr("user");
		customer.setAut_cust_id(user.getCustId());
		return GeneralUtils.easyUI(customerMapper.count(customer), customerMapper.selectList(customer));
	}

	/**
	 * 调研网站后台管理员查看网站注册用户
	 * 
	 * @param customer
	 * @return
	 * @throws Exception
	 */
	public String searchCustomerByAdmin(Customer customer) throws Exception {
		return null;
	}

	public void setCustomerMapper(CustomerMapper customerMapper) {
		this.customerMapper = customerMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}
}
